package com.wang.gulimall.order.web;

import com.wang.common.exception.NoStockException;
import com.wang.gulimall.order.service.OrderService;
import com.wang.gulimall.order.vo.OrderConfirmVo;
import com.wang.gulimall.order.vo.OrderSubmitVo;
import com.wang.gulimall.order.vo.SubmitOrderResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutionException;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2021/6/11 21:05
 */

@Controller
@Slf4j
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * 去结算确认页 生成令牌 收货地址 购物车项目  线程池异步编排
     * @param model
     * @param request
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping(value = "/toTrade")
    public String toTrade(Model model, HttpServletRequest request) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("confirmOrderData",confirmVo);
        return "confirm";
    }

    /**
     * 提交订单操作
     * @param vo
     * @return
     */

    @PostMapping(value = "/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        try {
            // 创建订单
            log.error("orderService.createSubmitOrde");
            SubmitOrderResponseVo responseVo = orderService.createSubmitOrder(vo);
            //订单反应码为0的话表示成功
            if (responseVo.getCode() == 0) {
                log.error("订单反应码为0的话表示成功");
                model.addAttribute("submitOrderResp", responseVo);
                return "pay";
            } else {
                log.error("下单失败"+responseVo.getCode());
                String msg = "下单失败";
                switch (responseVo.getCode()) {
                    case 1:
                        msg += "令牌订单信息过期，请刷新再次提交";
                        break;
                    case 2:
                        msg += "订单商品价格发生变化，请确认后再次提交";
                        break;
                    case 3:
                        msg += "库存锁定失败，商品库存不足";
                        break;
                }
                redirectAttributes.addFlashAttribute("msg", msg);
                return "redirect:http://order.gulimall.com/toTrade";
            }
        } catch (Exception e) {
            if (e instanceof NoStockException) {
                String message = ((NoStockException) e).getMessage();
                redirectAttributes.addFlashAttribute("msg", message);
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
